// pages/login/index.js
const {
  wxMobileLogin,
  decodeUserInfo
} = require('../../api/login')
const config = require("../../config/config");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree: true
  },
  onLoad(options) {
    this.login()
    var that = this
    this.data.timer = setInterval(function () {
      that.login()
    }, 240000)
  },
  changeStatus(e) {
    this.setData({
      isAgree: !this.data.isAgree
    })
  },
  back(){
    wx.navigateBack()
  },
  checkIsAgree() {
    const {
      isAgree
    } = this.data
    if (!isAgree) {
      wx.showToast({
        title: '请先同意用户服务协议及隐私政策',
        icon: 'none'
      })
      return
    }
  },
  async getPhoneNumber(e) {
    var that = this
    if (!e.detail.iv) {
      that.resolvePrivacyAuthorization({
        event: 'disagree'
      })
      return
    }
    wx.showLoading({
      title: '授权中',
    })
    const {
      encryptedData,
      iv
    } = e.detail
    var {
      code
    } = this.data
    const param = {
      code,
      encryptedData,
      iv,
      promoteUser: wx.getStorageSync('promoteUser'),
      spreadUserId: wx.getStorageSync('spreadUserId')
    }
    console.log(param)
    const data = await wxMobileLogin(param)
    wx.hideLoading()
    if (data.success) {
      const {
        userInfo,
        token
      } = data.result
      let newTime = Math.round(new Date())
      wx.setStorageSync(config.storageKey.loginTime, newTime);
      wx.setStorageSync(config.storageKey.token, token)
      that.fetchUserInfo()
      wx.hideLoading()
      if (!userInfo.mobile == '') {
        wx.setStorageSync(config.storageKey.mobile, userInfo.mobile)
      }
    }
  },
  handleAgreePrivacyAuthorization() {
    this.resolvePrivacyAuthorization({
      buttonId: 'agree-btn',
      event: 'agree'
    })
  },
  goto(e) {
    const {
      url
    } = e.currentTarget.dataset
    wx.navigateTo({
      url,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */

  login() {
    wx.login({
      success: res => {
        this.setData({
          code: res.code
        })
      }
    })
  },
  async fetchUserInfo() {
    const {
      result
    } = await decodeUserInfo()
    wx.setStorageSync(config.storageKey.userInfo, result);
    const page = getCurrentPages()
    const prevPage = page[page.length - 2]
    prevPage.onLoadFunc()
    wx.navigateBack()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.onNeedPrivacyAuthorization) {
      wx.onNeedPrivacyAuthorization(resolve => {
        // this.selectComponent('#agreement').show();
        this.resolvePrivacyAuthorization = resolve
      })
      wx.requirePrivacyAuthorize({
        success: res => {},
        fail: () => {},
        complete: () => {}
      })
    }
  },
  refuse(e) {
    this.resolvePrivacyAuthorization({
      event: 'disagree'
    })
  },
  agree(e) {
    this.resolvePrivacyAuthorization({
      buttonId: 'agree-btn',
      event: 'agree'
    })
    this.setData({
      isAgree: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearInterval(this.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})
